package com.limh.readers.activity

import android.app.ProgressDialog
import android.os.Handler
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.limh.readers.R
import com.limh.readers.bean.Result
import com.limh.readers.net.BaseRetrofit
import com.limh.readers.net.ServerApi
import com.limh.readers.utils.Utils
import com.limh.readers.utils.GsonUtils
import com.limh.readers.utils.LogUtil
import com.limh.readers.utils.SharePreferUtils
import rx.schedulers.Schedulers

/**
 * Function:
 * author: limh
 * time:2017/7/21
 */

class RegterActivity : BaseActivity() {
    @BindView(R.id.edit_username)
    lateinit var txtName: TextView
    @BindView(R.id.edit_email)
    lateinit var txtEmail: TextView
    @BindView(R.id.edit_pass1)
    lateinit var txtPass1: TextView
    @BindView(R.id.edit_pass2)
    lateinit var txtPass2: TextView

    private lateinit var dialog: ProgressDialog

    private val TAG: String = "RegterActivity"

    override fun findView() {
        setContentView(R.layout.activity_regiter)
    }

    override fun initViews() {
    }

    override fun initDatas() {
    }

    @OnClick(R.id.image_back, R.id.btn_register)
    fun OnClick(view: View) {
        when (view.id) {
            R.id.image_back -> finish()
            R.id.btn_register -> register()
        }
    }

    private fun register() {
        if (TextUtils.isEmpty(txtName.text)) {
            showToastShort("昵称不能为空")
            return
        }
        if (TextUtils.isEmpty(txtEmail.text)) {
            showToastShort("邮箱不能为空")
            return
        }
        if (TextUtils.isEmpty(txtPass1.text) || TextUtils.isEmpty(txtPass2.text)) {
            showToastShort("密码不能为空")
            return
        }
        if (txtPass1.text.toString() != txtPass2.text.toString()) {
            showToastShort("两次密码不一致")
            return
        }
        if(!Utils.isEmail(txtEmail.text.toString())){
            showToastShort("请输入正确的邮箱地址")
            return
        }
        dialog = ProgressDialog(this)
        dialog.setMessage("注册中，请稍后...")
        dialog.show()

        val params = mapOf(Pair("username", txtName.text.toString()),
                Pair("email", txtEmail.text.toString()), Pair("password", txtPass1.text.toString()))
        LogUtil.w(TAG, "params=" + params.toString())
        val service: ServerApi = BaseRetrofit().createService(ServerApi::class.java)
        service.regidter(params)
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        { response ->
                            sendMsg(GsonUtils.Gson2Str(response), 0x123, handler)
                            LogUtil.d(TAG, response.toString())
                        },
                        { t: Throwable? ->
                            sendMsg("注册失败", 0x124, handler)
                            LogUtil.d(TAG, t.toString())
                        },
                        { })
    }

    private val handler = Handler(Handler.Callback { msg ->
        when (msg.what) {
            0x123 -> {
                val rep = GsonUtils.str2Bean(msg.data.getString("msg"), Result::class.java)
                showToastShort(rep.getMessage()!!)
                LogUtil.d(TAG,rep.getData().toString())
                SharePreferUtils.put(this, "user", rep.getData().toString())
                finish()
            }
            else -> showToastShort(msg.data.getString("msg"))
        }
        if (dialog.isShowing) dialog.dismiss()
        false
    })
}